<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>events_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_events_events_test.js.html">events_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.eventsTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.eventsTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.asserts.AssertionError&#39;);
<a name="line19"></a>goog.require(&#39;goog.debug.EntryPointMonitor&#39;);
<a name="line20"></a>goog.require(&#39;goog.debug.ErrorHandler&#39;);
<a name="line21"></a>goog.require(&#39;goog.debug.entryPointRegistry&#39;);
<a name="line22"></a>goog.require(&#39;goog.dom&#39;);
<a name="line23"></a>goog.require(&#39;goog.events&#39;);
<a name="line24"></a>goog.require(&#39;goog.events.BrowserFeature&#39;);
<a name="line25"></a>goog.require(&#39;goog.events.CaptureSimulationMode&#39;);
<a name="line26"></a>goog.require(&#39;goog.events.Event&#39;);
<a name="line27"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line28"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line29"></a>goog.require(&#39;goog.events.Listener&#39;);
<a name="line30"></a>goog.require(&#39;goog.functions&#39;);
<a name="line31"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line32"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line33"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line34"></a>
<a name="line35"></a>var originalHandleBrowserEvent = goog.events.handleBrowserEvent_;
<a name="line36"></a>var propertyReplacer;
<a name="line37"></a>var et1, et2, et3;
<a name="line38"></a>
<a name="line39"></a>function setUp() {
<a name="line40"></a>  et1 = new goog.events.EventTarget();
<a name="line41"></a>  et2 = new goog.events.EventTarget();
<a name="line42"></a>  et3 = new goog.events.EventTarget();
<a name="line43"></a>  propertyReplacer = new goog.testing.PropertyReplacer();
<a name="line44"></a>}
<a name="line45"></a>
<a name="line46"></a>function tearDown() {
<a name="line47"></a>  goog.events.CAPTURE_SIMULATION_MODE =
<a name="line48"></a>      goog.events.CaptureSimulationMode.ON;
<a name="line49"></a>  goog.events.handleBrowserEvent_ = originalHandleBrowserEvent;
<a name="line50"></a>  goog.disposeAll(et1, et2, et3);
<a name="line51"></a>  goog.events.removeAll(document.body);
<a name="line52"></a>  propertyReplacer.reset();
<a name="line53"></a>}
<a name="line54"></a>
<a name="line55"></a>function testProtectBrowserEventEntryPoint() {
<a name="line56"></a>  var errorHandlerFn = goog.testing.recordFunction();
<a name="line57"></a>  var errorHandler = new goog.debug.ErrorHandler(errorHandlerFn);
<a name="line58"></a>
<a name="line59"></a>  goog.events.protectBrowserEventEntryPoint(errorHandler);
<a name="line60"></a>
<a name="line61"></a>  var browserEventHandler =
<a name="line62"></a>      goog.testing.recordFunction(goog.events.handleBrowserEvent_);
<a name="line63"></a>  goog.events.handleBrowserEvent_ = function() {
<a name="line64"></a>    try {
<a name="line65"></a>      browserEventHandler.apply(this, arguments);
<a name="line66"></a>    } catch (e) {
<a name="line67"></a>      // Ignored.
<a name="line68"></a>    }
<a name="line69"></a>  };
<a name="line70"></a>
<a name="line71"></a>  var err = Error(&#39;test&#39;);
<a name="line72"></a>  var body = document.body;
<a name="line73"></a>  goog.events.listen(body, goog.events.EventType.CLICK, function() {
<a name="line74"></a>    throw err;
<a name="line75"></a>  });
<a name="line76"></a>
<a name="line77"></a>  dispatchClick(body);
<a name="line78"></a>
<a name="line79"></a>  assertEquals(&#39;Error handler callback should be called.&#39;,
<a name="line80"></a>      1, errorHandlerFn.getCallCount());
<a name="line81"></a>  assertEquals(err, errorHandlerFn.getLastCall().getArgument(0));
<a name="line82"></a>
<a name="line83"></a>  assertEquals(1, browserEventHandler.getCallCount());
<a name="line84"></a>  var err2 = browserEventHandler.getLastCall().getError();
<a name="line85"></a>  assertNotNull(err2);
<a name="line86"></a>  assertTrue(
<a name="line87"></a>      err2 instanceof goog.debug.ErrorHandler.ProtectedFunctionError);
<a name="line88"></a>}
<a name="line89"></a>
<a name="line90"></a>function testSelfRemove() {
<a name="line91"></a>  var callback = function() {
<a name="line92"></a>    // This listener removes itself during event dispatching, so it
<a name="line93"></a>    // is marked as &#39;removed&#39; but not actually removed until after event
<a name="line94"></a>    // dispatching ends.
<a name="line95"></a>    goog.events.removeAll(et1, &#39;click&#39;);
<a name="line96"></a>
<a name="line97"></a>    // Test that goog.events.getListener ignores events marked as &#39;removed&#39;.
<a name="line98"></a>    assertNull(goog.events.getListener(et1, &#39;click&#39;, callback));
<a name="line99"></a>  };
<a name="line100"></a>  var key = goog.events.listen(et1, &#39;click&#39;, callback);
<a name="line101"></a>  goog.events.dispatchEvent(et1, &#39;click&#39;);
<a name="line102"></a>}
<a name="line103"></a>
<a name="line104"></a>function testHasListener() {
<a name="line105"></a>  var div = document.createElement(&#39;div&#39;);
<a name="line106"></a>  assertFalse(goog.events.hasListener(div));
<a name="line107"></a>
<a name="line108"></a>  var key = goog.events.listen(div, &#39;click&#39;, function() {});
<a name="line109"></a>  assertTrue(goog.events.hasListener(div));
<a name="line110"></a>  assertTrue(goog.events.hasListener(div, &#39;click&#39;));
<a name="line111"></a>  assertTrue(goog.events.hasListener(div, &#39;click&#39;, false));
<a name="line112"></a>  assertTrue(goog.events.hasListener(div, undefined, false));
<a name="line113"></a>
<a name="line114"></a>  assertFalse(goog.events.hasListener(div, &#39;click&#39;, true));
<a name="line115"></a>  assertFalse(goog.events.hasListener(div, undefined, true));
<a name="line116"></a>  assertFalse(goog.events.hasListener(div, &#39;mouseup&#39;));
<a name="line117"></a>
<a name="line118"></a>  // Test that hasListener returns false when all listeners are removed.
<a name="line119"></a>  goog.events.unlistenByKey(key);
<a name="line120"></a>  assertFalse(goog.events.hasListener(div));
<a name="line121"></a>}
<a name="line122"></a>
<a name="line123"></a>function testHasListenerWithEventTarget() {
<a name="line124"></a>  assertFalse(goog.events.hasListener(et1));
<a name="line125"></a>
<a name="line126"></a>  function callback() {};
<a name="line127"></a>  goog.events.listen(et1, &#39;test&#39;, callback, true);
<a name="line128"></a>  assertTrue(goog.events.hasListener(et1));
<a name="line129"></a>  assertTrue(goog.events.hasListener(et1, &#39;test&#39;));
<a name="line130"></a>  assertTrue(goog.events.hasListener(et1, &#39;test&#39;, true));
<a name="line131"></a>  assertTrue(goog.events.hasListener(et1, undefined, true));
<a name="line132"></a>
<a name="line133"></a>  assertFalse(goog.events.hasListener(et1, &#39;click&#39;));
<a name="line134"></a>  assertFalse(goog.events.hasListener(et1, &#39;test&#39;, false));
<a name="line135"></a>
<a name="line136"></a>  goog.events.unlisten(et1, &#39;test&#39;, callback, true);
<a name="line137"></a>  assertFalse(goog.events.hasListener(et1));
<a name="line138"></a>}
<a name="line139"></a>
<a name="line140"></a>function testHasListenerWithMultipleTargets() {
<a name="line141"></a>  function callback() {};
<a name="line142"></a>
<a name="line143"></a>  goog.events.listen(et1, &#39;test1&#39;, callback, true);
<a name="line144"></a>  goog.events.listen(et2, &#39;test2&#39;, callback, true);
<a name="line145"></a>
<a name="line146"></a>  assertTrue(goog.events.hasListener(et1));
<a name="line147"></a>  assertTrue(goog.events.hasListener(et2));
<a name="line148"></a>  assertTrue(goog.events.hasListener(et1, &#39;test1&#39;));
<a name="line149"></a>  assertTrue(goog.events.hasListener(et2, &#39;test2&#39;));
<a name="line150"></a>
<a name="line151"></a>  assertFalse(goog.events.hasListener(et1, &#39;et2&#39;));
<a name="line152"></a>  assertFalse(goog.events.hasListener(et2, &#39;et1&#39;));
<a name="line153"></a>
<a name="line154"></a>  goog.events.removeAll(et1);
<a name="line155"></a>  goog.events.removeAll(et2);
<a name="line156"></a>}
<a name="line157"></a>
<a name="line158"></a>function testBubbleSingle() {
<a name="line159"></a>  et1.setParentEventTarget(et2);
<a name="line160"></a>  et2.setParentEventTarget(et3);
<a name="line161"></a>
<a name="line162"></a>  var count = 0;
<a name="line163"></a>  function callback() {
<a name="line164"></a>    count++;
<a name="line165"></a>  }
<a name="line166"></a>
<a name="line167"></a>  goog.events.listen(et3, &#39;test&#39;, callback, false);
<a name="line168"></a>
<a name="line169"></a>  et1.dispatchEvent(&#39;test&#39;);
<a name="line170"></a>
<a name="line171"></a>  assertEquals(1, count);
<a name="line172"></a>
<a name="line173"></a>  goog.events.removeAll(et1);
<a name="line174"></a>  goog.events.removeAll(et2);
<a name="line175"></a>  goog.events.removeAll(et3);
<a name="line176"></a>}
<a name="line177"></a>
<a name="line178"></a>function testCaptureSingle() {
<a name="line179"></a>  et1.setParentEventTarget(et2);
<a name="line180"></a>  et2.setParentEventTarget(et3);
<a name="line181"></a>
<a name="line182"></a>  var count = 0;
<a name="line183"></a>  function callback() {
<a name="line184"></a>    count++;
<a name="line185"></a>  }
<a name="line186"></a>
<a name="line187"></a>  goog.events.listen(et3, &#39;test&#39;, callback, true);
<a name="line188"></a>
<a name="line189"></a>  et1.dispatchEvent(&#39;test&#39;);
<a name="line190"></a>
<a name="line191"></a>  assertEquals(1, count);
<a name="line192"></a>
<a name="line193"></a>  goog.events.removeAll(et1);
<a name="line194"></a>  goog.events.removeAll(et2);
<a name="line195"></a>  goog.events.removeAll(et3);
<a name="line196"></a>}
<a name="line197"></a>
<a name="line198"></a>function testCaptureAndBubble() {
<a name="line199"></a>  et1.setParentEventTarget(et2);
<a name="line200"></a>  et2.setParentEventTarget(et3);
<a name="line201"></a>
<a name="line202"></a>  var count = 0;
<a name="line203"></a>  function callbackCapture1() {
<a name="line204"></a>    count++;
<a name="line205"></a>    assertEquals(3, count);
<a name="line206"></a>  }
<a name="line207"></a>  function callbackBubble1() {
<a name="line208"></a>    count++;
<a name="line209"></a>    assertEquals(4, count);
<a name="line210"></a>  }
<a name="line211"></a>
<a name="line212"></a>  function callbackCapture2() {
<a name="line213"></a>    count++;
<a name="line214"></a>    assertEquals(2, count);
<a name="line215"></a>  }
<a name="line216"></a>  function callbackBubble2() {
<a name="line217"></a>    count++;
<a name="line218"></a>    assertEquals(5, count);
<a name="line219"></a>  }
<a name="line220"></a>
<a name="line221"></a>  function callbackCapture3() {
<a name="line222"></a>    count++;
<a name="line223"></a>    assertEquals(1, count);
<a name="line224"></a>  }
<a name="line225"></a>  function callbackBubble3() {
<a name="line226"></a>    count++;
<a name="line227"></a>    assertEquals(6, count);
<a name="line228"></a>  }
<a name="line229"></a>
<a name="line230"></a>  goog.events.listen(et1, &#39;test&#39;, callbackCapture1, true);
<a name="line231"></a>  goog.events.listen(et1, &#39;test&#39;, callbackBubble1, false);
<a name="line232"></a>  goog.events.listen(et2, &#39;test&#39;, callbackCapture2, true);
<a name="line233"></a>  goog.events.listen(et2, &#39;test&#39;, callbackBubble2, false);
<a name="line234"></a>  goog.events.listen(et3, &#39;test&#39;, callbackCapture3, true);
<a name="line235"></a>  goog.events.listen(et3, &#39;test&#39;, callbackBubble3, false);
<a name="line236"></a>
<a name="line237"></a>  et1.dispatchEvent(&#39;test&#39;);
<a name="line238"></a>
<a name="line239"></a>  assertEquals(6, count);
<a name="line240"></a>
<a name="line241"></a>  goog.events.removeAll(et1);
<a name="line242"></a>  goog.events.removeAll(et2);
<a name="line243"></a>  goog.events.removeAll(et3);
<a name="line244"></a>}
<a name="line245"></a>
<a name="line246"></a>function testCapturingRemovesBubblingListener() {
<a name="line247"></a>  var bubbleCount = 0;
<a name="line248"></a>  function callbackBubble() {
<a name="line249"></a>    bubbleCount++;
<a name="line250"></a>  }
<a name="line251"></a>
<a name="line252"></a>  var captureCount = 0;
<a name="line253"></a>  function callbackCapture() {
<a name="line254"></a>    captureCount++;
<a name="line255"></a>    goog.events.removeAll(et1);
<a name="line256"></a>  }
<a name="line257"></a>
<a name="line258"></a>  goog.events.listen(et1, &#39;test&#39;, callbackCapture, true);
<a name="line259"></a>  goog.events.listen(et1, &#39;test&#39;, callbackBubble, false);
<a name="line260"></a>
<a name="line261"></a>  et1.dispatchEvent(&#39;test&#39;);
<a name="line262"></a>  assertEquals(1, captureCount);
<a name="line263"></a>  assertEquals(0, bubbleCount);
<a name="line264"></a>}
<a name="line265"></a>
<a name="line266"></a>function dispatchClick(target) {
<a name="line267"></a>  if (target.click) {
<a name="line268"></a>    target.click();
<a name="line269"></a>  } else {
<a name="line270"></a>    var e = document.createEvent(&#39;MouseEvents&#39;);
<a name="line271"></a>    e.initMouseEvent(&#39;click&#39;, true, true, window, 0, 0, 0, 0, 0, false,
<a name="line272"></a>        false, false, false, 0, null);
<a name="line273"></a>    target.dispatchEvent(e);
<a name="line274"></a>  }
<a name="line275"></a>}
<a name="line276"></a>
<a name="line277"></a>function testHandleBrowserEventBubblingListener() {
<a name="line278"></a>  var count = 0;
<a name="line279"></a>  var body = document.body;
<a name="line280"></a>  goog.events.listen(body, &#39;click&#39;, function() {
<a name="line281"></a>    count++;
<a name="line282"></a>  });
<a name="line283"></a>  dispatchClick(body);
<a name="line284"></a>  assertEquals(1, count);
<a name="line285"></a>}
<a name="line286"></a>
<a name="line287"></a>function testHandleBrowserEventCapturingListener() {
<a name="line288"></a>  var count = 0;
<a name="line289"></a>  var body = document.body;
<a name="line290"></a>  goog.events.listen(body, &#39;click&#39;, function() {
<a name="line291"></a>    count++;
<a name="line292"></a>  }, true);
<a name="line293"></a>  dispatchClick(body);
<a name="line294"></a>  assertEquals(1, count);
<a name="line295"></a>}
<a name="line296"></a>
<a name="line297"></a>function testHandleBrowserEventCapturingAndBubblingListener() {
<a name="line298"></a>  var count = 1;
<a name="line299"></a>  var body = document.body;
<a name="line300"></a>  goog.events.listen(body, &#39;click&#39;, function() {
<a name="line301"></a>    count += 3;
<a name="line302"></a>  }, true);
<a name="line303"></a>  goog.events.listen(body, &#39;click&#39;, function() {
<a name="line304"></a>    count *= 5;
<a name="line305"></a>  }, false);
<a name="line306"></a>  dispatchClick(body);
<a name="line307"></a>  assertEquals(20, count);
<a name="line308"></a>}
<a name="line309"></a>
<a name="line310"></a>function testHandleBrowserEventCapturingRemovesBubblingListener() {
<a name="line311"></a>  var body = document.body;
<a name="line312"></a>
<a name="line313"></a>  var bubbleCount = 0;
<a name="line314"></a>  function callbackBubble() {
<a name="line315"></a>    bubbleCount++;
<a name="line316"></a>  }
<a name="line317"></a>
<a name="line318"></a>  var captureCount = 0;
<a name="line319"></a>  function callbackCapture() {
<a name="line320"></a>    captureCount++;
<a name="line321"></a>    goog.events.removeAll(body);
<a name="line322"></a>  }
<a name="line323"></a>
<a name="line324"></a>  goog.events.listen(body, &#39;click&#39;, callbackCapture, true);
<a name="line325"></a>  goog.events.listen(body, &#39;click&#39;, callbackBubble, false);
<a name="line326"></a>
<a name="line327"></a>  dispatchClick(body);
<a name="line328"></a>  assertEquals(1, captureCount);
<a name="line329"></a>  assertEquals(0, bubbleCount);
<a name="line330"></a>}
<a name="line331"></a>
<a name="line332"></a>function testHandleEventPropagationOnParentElement() {
<a name="line333"></a>  var count = 1;
<a name="line334"></a>  goog.events.listen(document.documentElement, &#39;click&#39;, function() {
<a name="line335"></a>    count += 3;
<a name="line336"></a>  }, true);
<a name="line337"></a>  goog.events.listen(document.documentElement, &#39;click&#39;, function() {
<a name="line338"></a>    count *= 5;
<a name="line339"></a>  }, false);
<a name="line340"></a>  dispatchClick(document.body);
<a name="line341"></a>  assertEquals(20, count);
<a name="line342"></a>}
<a name="line343"></a>
<a name="line344"></a>function testEntryPointRegistry() {
<a name="line345"></a>  var monitor = new goog.debug.EntryPointMonitor();
<a name="line346"></a>  var replacement = function() {};
<a name="line347"></a>  monitor.wrap = goog.testing.recordFunction(
<a name="line348"></a>      goog.functions.constant(replacement));
<a name="line349"></a>
<a name="line350"></a>  goog.debug.entryPointRegistry.monitorAll(monitor);
<a name="line351"></a>  assertTrue(monitor.wrap.getCallCount() &gt;= 1);
<a name="line352"></a>  assertEquals(replacement, goog.events.handleBrowserEvent_);
<a name="line353"></a>}
<a name="line354"></a>
<a name="line355"></a>// Fixes bug http://b/6434926
<a name="line356"></a>function testListenOnceHandlerDispatchCausingInfiniteLoop() {
<a name="line357"></a>  var handleFoo = goog.testing.recordFunction(function() {
<a name="line358"></a>    et1.dispatchEvent(&#39;foo&#39;);
<a name="line359"></a>  });
<a name="line360"></a>
<a name="line361"></a>  goog.events.listenOnce(et1, &#39;foo&#39;, handleFoo);
<a name="line362"></a>
<a name="line363"></a>  et1.dispatchEvent(&#39;foo&#39;);
<a name="line364"></a>
<a name="line365"></a>  assertEquals(&#39;Handler should be called only once.&#39;,
<a name="line366"></a>               1, handleFoo.getCallCount());
<a name="line367"></a>}
<a name="line368"></a>
<a name="line369"></a>function testCreationStack() {
<a name="line370"></a>  if (!new Error().stack)
<a name="line371"></a>    return;
<a name="line372"></a>  propertyReplacer.replace(goog.events.Listener, &#39;ENABLE_MONITORING&#39;, true);
<a name="line373"></a>
<a name="line374"></a>  var div = document.createElement(&#39;div&#39;);
<a name="line375"></a>  var key = goog.events.listen(
<a name="line376"></a>      div, goog.events.EventType.CLICK, goog.nullFunction);
<a name="line377"></a>  var listenerStack = key.creationStack;
<a name="line378"></a>
<a name="line379"></a>  // Check that the name of this test function occurs in the stack trace.
<a name="line380"></a>  assertContains(&#39;testCreationStack&#39;, listenerStack);
<a name="line381"></a>  goog.events.unlistenByKey(key);
<a name="line382"></a>}
<a name="line383"></a>
<a name="line384"></a>function testListenOnceAfterListenDoesNotChangeExistingListener() {
<a name="line385"></a>  var listener = goog.testing.recordFunction();
<a name="line386"></a>  goog.events.listen(document.body, &#39;click&#39;, listener);
<a name="line387"></a>  goog.events.listenOnce(document.body, &#39;click&#39;, listener);
<a name="line388"></a>
<a name="line389"></a>  dispatchClick(document.body);
<a name="line390"></a>  dispatchClick(document.body);
<a name="line391"></a>  dispatchClick(document.body);
<a name="line392"></a>
<a name="line393"></a>  assertEquals(3, listener.getCallCount());
<a name="line394"></a>}
<a name="line395"></a>
<a name="line396"></a>function testListenOnceAfterListenOnceDoesNotChangeExistingListener() {
<a name="line397"></a>  var listener = goog.testing.recordFunction();
<a name="line398"></a>  goog.events.listenOnce(document.body, &#39;click&#39;, listener);
<a name="line399"></a>  goog.events.listenOnce(document.body, &#39;click&#39;, listener);
<a name="line400"></a>
<a name="line401"></a>  dispatchClick(document.body);
<a name="line402"></a>  dispatchClick(document.body);
<a name="line403"></a>  dispatchClick(document.body);
<a name="line404"></a>
<a name="line405"></a>  assertEquals(1, listener.getCallCount());
<a name="line406"></a>}
<a name="line407"></a>
<a name="line408"></a>function testListenAfterListenOnceRemoveOnceness() {
<a name="line409"></a>  var listener = goog.testing.recordFunction();
<a name="line410"></a>  goog.events.listenOnce(document.body, &#39;click&#39;, listener);
<a name="line411"></a>  goog.events.listen(document.body, &#39;click&#39;, listener);
<a name="line412"></a>
<a name="line413"></a>  dispatchClick(document.body);
<a name="line414"></a>  dispatchClick(document.body);
<a name="line415"></a>  dispatchClick(document.body);
<a name="line416"></a>
<a name="line417"></a>  assertEquals(3, listener.getCallCount());
<a name="line418"></a>}
<a name="line419"></a>
<a name="line420"></a>function testUnlistenAfterListenOnce() {
<a name="line421"></a>  var listener = goog.testing.recordFunction();
<a name="line422"></a>
<a name="line423"></a>  goog.events.listenOnce(document.body, &#39;click&#39;, listener);
<a name="line424"></a>  goog.events.unlisten(document.body, &#39;click&#39;, listener);
<a name="line425"></a>  dispatchClick(document.body);
<a name="line426"></a>
<a name="line427"></a>  goog.events.listenOnce(document.body, &#39;click&#39;, listener);
<a name="line428"></a>  goog.events.listen(document.body, &#39;click&#39;, listener);
<a name="line429"></a>  goog.events.unlisten(document.body, &#39;click&#39;, listener);
<a name="line430"></a>  dispatchClick(document.body);
<a name="line431"></a>
<a name="line432"></a>  goog.events.listen(document.body, &#39;click&#39;, listener);
<a name="line433"></a>  goog.events.listenOnce(document.body, &#39;click&#39;, listener);
<a name="line434"></a>  goog.events.unlisten(document.body, &#39;click&#39;, listener);
<a name="line435"></a>  dispatchClick(document.body);
<a name="line436"></a>
<a name="line437"></a>  goog.events.listenOnce(document.body, &#39;click&#39;, listener);
<a name="line438"></a>  goog.events.listenOnce(document.body, &#39;click&#39;, listener);
<a name="line439"></a>  goog.events.unlisten(document.body, &#39;click&#39;, listener);
<a name="line440"></a>  dispatchClick(document.body);
<a name="line441"></a>
<a name="line442"></a>  assertEquals(0, listener.getCallCount());
<a name="line443"></a>}
<a name="line444"></a>
<a name="line445"></a>function testEventBubblingWithReentrantDispatch_bubbling() {
<a name="line446"></a>  runEventPropogationWithReentrantDispatch(false);
<a name="line447"></a>}
<a name="line448"></a>
<a name="line449"></a>function testEventBubblingWithReentrantDispatch_capture() {
<a name="line450"></a>  runEventPropogationWithReentrantDispatch(true);
<a name="line451"></a>}
<a name="line452"></a>
<a name="line453"></a>function runEventPropogationWithReentrantDispatch(useCapture) {
<a name="line454"></a>  var eventType = &#39;test-event-type&#39;;
<a name="line455"></a>
<a name="line456"></a>  var child = et1;
<a name="line457"></a>  var parent = et2;
<a name="line458"></a>  child.setParentEventTarget(parent);
<a name="line459"></a>
<a name="line460"></a>  var firstTarget = useCapture ? parent : child;
<a name="line461"></a>  var secondTarget = useCapture ? child : parent;
<a name="line462"></a>
<a name="line463"></a>  var firstListener = function(evt) {
<a name="line464"></a>    if (evt.isFirstEvent) {
<a name="line465"></a>      // Fires another event of the same type the first time it is invoked.
<a name="line466"></a>      child.dispatchEvent(new goog.events.Event(eventType));
<a name="line467"></a>    }
<a name="line468"></a>  };
<a name="line469"></a>  goog.events.listen(firstTarget, eventType, firstListener, useCapture);
<a name="line470"></a>
<a name="line471"></a>  var secondListener = goog.testing.recordFunction();
<a name="line472"></a>  goog.events.listen(secondTarget, eventType, secondListener, useCapture);
<a name="line473"></a>
<a name="line474"></a>  // Fire the first event.
<a name="line475"></a>  var firstEvent = new goog.events.Event(eventType);
<a name="line476"></a>  firstEvent.isFirstEvent = true;
<a name="line477"></a>  child.dispatchEvent(firstEvent);
<a name="line478"></a>
<a name="line479"></a>  assertEquals(2, secondListener.getCallCount());
<a name="line480"></a>}
<a name="line481"></a>
<a name="line482"></a>function testEventPropogationWhenListenerRemoved_bubbling() {
<a name="line483"></a>  runEventPropogationWhenListenerRemoved(false);
<a name="line484"></a>}
<a name="line485"></a>
<a name="line486"></a>function testEventPropogationWhenListenerRemoved_capture() {
<a name="line487"></a>  runEventPropogationWhenListenerRemoved(true);
<a name="line488"></a>}
<a name="line489"></a>
<a name="line490"></a>function runEventPropogationWhenListenerRemoved(useCapture) {
<a name="line491"></a>  var eventType = &#39;test-event-type&#39;;
<a name="line492"></a>
<a name="line493"></a>  var child = et1;
<a name="line494"></a>  var parent = et2;
<a name="line495"></a>  child.setParentEventTarget(parent);
<a name="line496"></a>
<a name="line497"></a>  var firstTarget = useCapture ? parent : child;
<a name="line498"></a>  var secondTarget = useCapture ? child : parent;
<a name="line499"></a>
<a name="line500"></a>  var firstListener = goog.testing.recordFunction();
<a name="line501"></a>  var secondListener = goog.testing.recordFunction();
<a name="line502"></a>  goog.events.listenOnce(firstTarget, eventType, firstListener, useCapture);
<a name="line503"></a>  goog.events.listen(secondTarget, eventType, secondListener, useCapture);
<a name="line504"></a>
<a name="line505"></a>  child.dispatchEvent(new goog.events.Event(eventType));
<a name="line506"></a>
<a name="line507"></a>  assertEquals(1, secondListener.getCallCount());
<a name="line508"></a>}
<a name="line509"></a>
<a name="line510"></a>function testEventPropogationWhenListenerAdded_bubbling() {
<a name="line511"></a>  runEventPropogationWhenListenerAdded(false);
<a name="line512"></a>}
<a name="line513"></a>
<a name="line514"></a>function testEventPropogationWhenListenerAdded_capture() {
<a name="line515"></a>  runEventPropogationWhenListenerAdded(true);
<a name="line516"></a>}
<a name="line517"></a>
<a name="line518"></a>function runEventPropogationWhenListenerAdded(useCapture) {
<a name="line519"></a>  var eventType = &#39;test-event-type&#39;;
<a name="line520"></a>
<a name="line521"></a>  var child = et1;
<a name="line522"></a>  var parent = et2;
<a name="line523"></a>  child.setParentEventTarget(parent);
<a name="line524"></a>
<a name="line525"></a>  var firstTarget = useCapture ? parent : child;
<a name="line526"></a>  var secondTarget = useCapture ? child : parent;
<a name="line527"></a>
<a name="line528"></a>  var firstListener = function() {
<a name="line529"></a>    goog.events.listen(secondTarget, eventType, secondListener, useCapture);
<a name="line530"></a>  };
<a name="line531"></a>  var secondListener = goog.testing.recordFunction();
<a name="line532"></a>  goog.events.listen(firstTarget, eventType, firstListener, useCapture);
<a name="line533"></a>
<a name="line534"></a>  child.dispatchEvent(new goog.events.Event(eventType));
<a name="line535"></a>
<a name="line536"></a>  assertEquals(1, secondListener.getCallCount());
<a name="line537"></a>}
<a name="line538"></a>
<a name="line539"></a>function testEventPropogationWhenListenerAddedAndRemoved_bubbling() {
<a name="line540"></a>  runEventPropogationWhenListenerAddedAndRemoved(false);
<a name="line541"></a>}
<a name="line542"></a>
<a name="line543"></a>function testEventPropogationWhenListenerAddedAndRemoved_capture() {
<a name="line544"></a>  runEventPropogationWhenListenerAddedAndRemoved(true);
<a name="line545"></a>}
<a name="line546"></a>
<a name="line547"></a>function runEventPropogationWhenListenerAddedAndRemoved(useCapture) {
<a name="line548"></a>  var eventType = &#39;test-event-type&#39;;
<a name="line549"></a>
<a name="line550"></a>  var child = et1;
<a name="line551"></a>  var parent = et2;
<a name="line552"></a>  child.setParentEventTarget(parent);
<a name="line553"></a>
<a name="line554"></a>  var firstTarget = useCapture ? parent : child;
<a name="line555"></a>  var secondTarget = useCapture ? child : parent;
<a name="line556"></a>
<a name="line557"></a>  var firstListener = function() {
<a name="line558"></a>    goog.events.listen(secondTarget, eventType, secondListener, useCapture);
<a name="line559"></a>  };
<a name="line560"></a>  var secondListener = goog.testing.recordFunction();
<a name="line561"></a>  goog.events.listenOnce(firstTarget, eventType, firstListener, useCapture);
<a name="line562"></a>
<a name="line563"></a>  child.dispatchEvent(new goog.events.Event(eventType));
<a name="line564"></a>
<a name="line565"></a>  assertEquals(1, secondListener.getCallCount());
<a name="line566"></a>}
<a name="line567"></a>
<a name="line568"></a>function testAssertWhenUsedWithUninitializedCustomEventTarget() {
<a name="line569"></a>  var SubClass = function() { /* does not call superclass ctor */ };
<a name="line570"></a>  goog.inherits(SubClass, goog.events.EventTarget);
<a name="line571"></a>
<a name="line572"></a>  var instance = new SubClass();
<a name="line573"></a>
<a name="line574"></a>  var e;
<a name="line575"></a>  e = assertThrows(function() {
<a name="line576"></a>    goog.events.listen(instance, &#39;test1&#39;, function() {});
<a name="line577"></a>  });
<a name="line578"></a>  assertTrue(e instanceof goog.asserts.AssertionError);
<a name="line579"></a>  e = assertThrows(function() {
<a name="line580"></a>    goog.events.dispatchEvent(instance, &#39;test1&#39;);
<a name="line581"></a>  });
<a name="line582"></a>  assertTrue(e instanceof goog.asserts.AssertionError);
<a name="line583"></a>  e = assertThrows(function() {
<a name="line584"></a>    instance.dispatchEvent(&#39;test1&#39;);
<a name="line585"></a>  });
<a name="line586"></a>  assertTrue(e instanceof goog.asserts.AssertionError);
<a name="line587"></a>}
<a name="line588"></a>
<a name="line589"></a>function testAssertWhenDispatchEventIsUsedWithNonCustomEventTarget() {
<a name="line590"></a>  var obj = {};
<a name="line591"></a>  e = assertThrows(function() {
<a name="line592"></a>    goog.events.dispatchEvent(obj, &#39;test1&#39;);
<a name="line593"></a>  });
<a name="line594"></a>  assertTrue(e instanceof goog.asserts.AssertionError);
<a name="line595"></a>}
<a name="line596"></a>
<a name="line597"></a>
<a name="line598"></a>function testPropagationStoppedDuringCapture() {
<a name="line599"></a>  var captureHandler = goog.testing.recordFunction(function(e) {
<a name="line600"></a>    e.stopPropagation();
<a name="line601"></a>  });
<a name="line602"></a>  var bubbleHandler = goog.testing.recordFunction();
<a name="line603"></a>
<a name="line604"></a>  var body = document.body;
<a name="line605"></a>  var div = document.createElement(&#39;div&#39;);
<a name="line606"></a>  body.appendChild(div);
<a name="line607"></a>  try {
<a name="line608"></a>    goog.events.listen(body, &#39;click&#39;, captureHandler, true);
<a name="line609"></a>    goog.events.listen(div, &#39;click&#39;, bubbleHandler, false);
<a name="line610"></a>    goog.events.listen(body, &#39;click&#39;, bubbleHandler, false);
<a name="line611"></a>
<a name="line612"></a>    dispatchClick(div);
<a name="line613"></a>    assertEquals(1, captureHandler.getCallCount());
<a name="line614"></a>    assertEquals(0, bubbleHandler.getCallCount());
<a name="line615"></a>
<a name="line616"></a>    goog.events.unlisten(body, &#39;click&#39;, captureHandler, true);
<a name="line617"></a>
<a name="line618"></a>    dispatchClick(div);
<a name="line619"></a>    assertEquals(2, bubbleHandler.getCallCount());
<a name="line620"></a>  } finally {
<a name="line621"></a>    goog.dom.removeNode(div);
<a name="line622"></a>    goog.events.removeAll(body);
<a name="line623"></a>    goog.events.removeAll(div);
<a name="line624"></a>  }
<a name="line625"></a>}
<a name="line626"></a>
<a name="line627"></a>function testPropagationStoppedDuringBubble() {
<a name="line628"></a>  var captureHandler = goog.testing.recordFunction();
<a name="line629"></a>  var bubbleHandler1 = goog.testing.recordFunction(function(e) {
<a name="line630"></a>    e.stopPropagation();
<a name="line631"></a>  });
<a name="line632"></a>  var bubbleHandler2 = goog.testing.recordFunction();
<a name="line633"></a>
<a name="line634"></a>  var body = document.body;
<a name="line635"></a>  var div = document.createElement(&#39;div&#39;);
<a name="line636"></a>  body.appendChild(div);
<a name="line637"></a>  try {
<a name="line638"></a>    goog.events.listen(body, &#39;click&#39;, captureHandler, true);
<a name="line639"></a>    goog.events.listen(div, &#39;click&#39;, bubbleHandler1, false);
<a name="line640"></a>    goog.events.listen(body, &#39;click&#39;, bubbleHandler2, false);
<a name="line641"></a>
<a name="line642"></a>    dispatchClick(div);
<a name="line643"></a>    assertEquals(1, captureHandler.getCallCount());
<a name="line644"></a>    assertEquals(1, bubbleHandler1.getCallCount());
<a name="line645"></a>    assertEquals(0, bubbleHandler2.getCallCount());
<a name="line646"></a>  } finally {
<a name="line647"></a>    goog.dom.removeNode(div);
<a name="line648"></a>    goog.events.removeAll(body);
<a name="line649"></a>    goog.events.removeAll(div);
<a name="line650"></a>  }
<a name="line651"></a>}
<a name="line652"></a>
<a name="line653"></a>function testAddingCaptureListenerDuringBubbleShouldNotFireTheListener() {
<a name="line654"></a>  var body = document.body;
<a name="line655"></a>  var div = document.createElement(&#39;div&#39;);
<a name="line656"></a>  body.appendChild(div);
<a name="line657"></a>
<a name="line658"></a>  var captureHandler1 = goog.testing.recordFunction();
<a name="line659"></a>  var captureHandler2 = goog.testing.recordFunction();
<a name="line660"></a>  var bubbleHandler = goog.testing.recordFunction(function(e) {
<a name="line661"></a>    goog.events.listen(body, &#39;click&#39;, captureHandler1, true);
<a name="line662"></a>    goog.events.listen(div, &#39;click&#39;, captureHandler2, true);
<a name="line663"></a>  });
<a name="line664"></a>
<a name="line665"></a>  try {
<a name="line666"></a>    goog.events.listen(div, &#39;click&#39;, bubbleHandler, false);
<a name="line667"></a>
<a name="line668"></a>    dispatchClick(div);
<a name="line669"></a>
<a name="line670"></a>    // These verify that the capture handlers registered in the bubble
<a name="line671"></a>    // handler is not invoked in the same event propagation phase.
<a name="line672"></a>    assertEquals(0, captureHandler1.getCallCount());
<a name="line673"></a>    assertEquals(0, captureHandler2.getCallCount());
<a name="line674"></a>    assertEquals(1, bubbleHandler.getCallCount());
<a name="line675"></a>  } finally {
<a name="line676"></a>    goog.dom.removeNode(div);
<a name="line677"></a>    goog.events.removeAll(body);
<a name="line678"></a>    goog.events.removeAll(div);
<a name="line679"></a>  }
<a name="line680"></a>}
<a name="line681"></a>
<a name="line682"></a>function testRemovingCaptureListenerDuringBubbleWouldNotFireListenerTwice() {
<a name="line683"></a>  var body = document.body;
<a name="line684"></a>  var div = document.createElement(&#39;div&#39;);
<a name="line685"></a>  body.appendChild(div);
<a name="line686"></a>
<a name="line687"></a>  var captureHandler = goog.testing.recordFunction();
<a name="line688"></a>  var bubbleHandler1 = goog.testing.recordFunction(function(e) {
<a name="line689"></a>    goog.events.unlisten(body, &#39;click&#39;, captureHandler, true);
<a name="line690"></a>  });
<a name="line691"></a>  var bubbleHandler2 = goog.testing.recordFunction();
<a name="line692"></a>
<a name="line693"></a>  try {
<a name="line694"></a>    goog.events.listen(body, &#39;click&#39;, captureHandler, true);
<a name="line695"></a>    goog.events.listen(div, &#39;click&#39;, bubbleHandler1, false);
<a name="line696"></a>    goog.events.listen(body, &#39;click&#39;, bubbleHandler2, false);
<a name="line697"></a>
<a name="line698"></a>    dispatchClick(div);
<a name="line699"></a>    assertEquals(1, captureHandler.getCallCount());
<a name="line700"></a>
<a name="line701"></a>    // Verify that neither of these handlers are called more than once.
<a name="line702"></a>    assertEquals(1, bubbleHandler1.getCallCount());
<a name="line703"></a>    assertEquals(1, bubbleHandler2.getCallCount());
<a name="line704"></a>  } finally {
<a name="line705"></a>    goog.dom.removeNode(div);
<a name="line706"></a>    goog.events.removeAll(body);
<a name="line707"></a>    goog.events.removeAll(div);
<a name="line708"></a>  }
<a name="line709"></a>}
<a name="line710"></a>
<a name="line711"></a>function testCaptureSimulationModeOffAndFail() {
<a name="line712"></a>  goog.events.CAPTURE_SIMULATION_MODE =
<a name="line713"></a>      goog.events.CaptureSimulationMode.OFF_AND_FAIL;
<a name="line714"></a>  var captureHandler = goog.testing.recordFunction();
<a name="line715"></a>
<a name="line716"></a>  if (!goog.events.BrowserFeature.HAS_W3C_EVENT_SUPPORT) {
<a name="line717"></a>    var err = assertThrows(function() {
<a name="line718"></a>      goog.events.listen(document.body, &#39;click&#39;, captureHandler, true);
<a name="line719"></a>    });
<a name="line720"></a>    assertTrue(err instanceof goog.asserts.AssertionError);
<a name="line721"></a>
<a name="line722"></a>    // Sanity tests.
<a name="line723"></a>    dispatchClick(document.body);
<a name="line724"></a>    assertEquals(0, captureHandler.getCallCount());
<a name="line725"></a>  } else {
<a name="line726"></a>    goog.events.listen(document.body, &#39;click&#39;, captureHandler, true);
<a name="line727"></a>    dispatchClick(document.body);
<a name="line728"></a>    assertEquals(1, captureHandler.getCallCount());
<a name="line729"></a>  }
<a name="line730"></a>}
<a name="line731"></a>
<a name="line732"></a>function testCaptureSimulationModeOffAndSilent() {
<a name="line733"></a>  goog.events.CAPTURE_SIMULATION_MODE =
<a name="line734"></a>      goog.events.CaptureSimulationMode.OFF_AND_SILENT;
<a name="line735"></a>  var captureHandler = goog.testing.recordFunction();
<a name="line736"></a>
<a name="line737"></a>  goog.events.listen(document.body, &#39;click&#39;, captureHandler, true);
<a name="line738"></a>  if (!goog.events.BrowserFeature.HAS_W3C_EVENT_SUPPORT) {
<a name="line739"></a>    dispatchClick(document.body);
<a name="line740"></a>    assertEquals(0, captureHandler.getCallCount());
<a name="line741"></a>  } else {
<a name="line742"></a>    dispatchClick(document.body);
<a name="line743"></a>    assertEquals(1, captureHandler.getCallCount());
<a name="line744"></a>  }
<a name="line745"></a>}
</pre>


</body>
</html>
